package com.kucai.yifaqapp.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.kucai.yifaqapp.R;
import com.kucai.yifaqapp.global.HttpAdress;
import com.kucai.yifaqapp.global.Info;
import com.kucai.yifaqapp.model.network.entity.res.Conform_psw_bean;
import com.kucai.yifaqapp.model.network.entity.res.Pay_ChangeMIMABean;
import com.kucai.yifaqapp.util.EventBus.MessageEvent;
import com.kucai.yifaqapp.util.Md5;
import com.kucai.yifaqapp.widget.BaseLoadingDialog;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

//修改取款密码
public class GetMoneyActivity extends BaseActivitySimple {
    //get_set_psw
    private EditText denglu;
    private EditText qukuan;
    private EditText qukuan2;
    private Button conform;
    private TextView title_left_tv;
    LinearLayout skinbg;
    private TextView headerTitle;
    private LinearLayout goback;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_get_money);
        denglu = findViewById(R.id.denglu);
        qukuan = findViewById(R.id.qukuan);
        qukuan2 = findViewById(R.id.qukuan2);
        conform = findViewById(R.id.conform);
        skinbg = findViewById(R.id.skinbg);
        headerTitle = findViewById(R.id.headerTitle);
        headerTitle.setText("设置密码");
        goback = findViewById(R.id.goback);
        goback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        title_left_tv = findViewById(R.id.title_left_tv);
        title_left_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        conform.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (denglu.getText().toString().isEmpty()) {
                    Toast.makeText(GetMoneyActivity.this, "请输入登录密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (qukuan.getText().toString().isEmpty()) {
                    Toast.makeText(GetMoneyActivity.this, "请输入登录密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (qukuan2.getText().toString().isEmpty()) {
                    Toast.makeText(GetMoneyActivity.this, "请输入登录密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!qukuan.getText().toString().trim().equals(qukuan2.getText().toString().trim())) {
                    Toast.makeText(GetMoneyActivity.this, "两次输入的密码需保持一致", Toast.LENGTH_SHORT).show();
                    return;
                }
                post_requst_paly();
            }
        });
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_get_money;
    }

    @Override
    protected void ChangeSkin() {
        switch (SkinId) {
            case 17771:
                skinbg.setBackgroundResource(R.mipmap.skin_bg1);
                break;
            case 17772:
                skinbg.setBackgroundResource(R.mipmap.skin_bg2);
                break;
            case 17773:
                skinbg.setBackgroundResource(R.mipmap.skin_bg3);
                break;
            default:
                skinbg.setBackgroundResource(R.mipmap.skin_bg1);
        }
    }

    Gson gson = new Gson();
    private BaseLoadingDialog dialog;

    public void post_requst_paly() {
        dialog = new BaseLoadingDialog(this, R.style.custom_dialog2);
        dialog.show();
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
                .readTimeout(20, TimeUnit.SECONDS)
                .build();
        Conform_psw_bean bean = new Conform_psw_bean();
        bean.loginPwd = Md5.toMd5(denglu.getText().toString());
        bean.fundPwd = Md5.toMd5(qukuan.getText().toString());
        String json = gson.toJson(bean);
        RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8")
                , json);
        final Request request = new Request.Builder()
                .url(HttpAdress.getuseAdress() + "api/m/user/saveFundPwd" + "?token=" + Info.user.token)//请求的url
                .post(requestBody)
                .build();

        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                dialog.dismiss();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final Pay_ChangeMIMABean bean1 = gson.fromJson(response.body().string(), Pay_ChangeMIMABean.class);
                if (bean1.getCode() == 0) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (dialog != null) {
                                dialog.dismiss();
                            }
                        }
                    });
                    Info.user.hasFundPwd = true;
                    EventBus.getDefault().post(new MessageEvent("200"));
                    finish();
                } else {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(GetMoneyActivity.this, bean1.getMsg(), Toast.LENGTH_LONG).show();
                            if (dialog != null) {
                                dialog.dismiss();
                            }
                        }
                    });
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (dialog != null) {
            dialog.dismiss();
        }
    }
}
